<?php
/**
 * 发起支付通知处理
 * User: Administrator
 * Date: 2018/6/25/025
 * Time: 22:46
 */

namespace app\lib\Order\OrderPaymentNotice;


class AddOrderNotice extends OrderNoticeBase
{
    /**
     * 发起支付通知申请(不需要申请的)
     * @return int|string
     * @throws \Exception
     */
    public function AddOrderNoPayNotice(){
        $model = $this->getTable();
        $data = $this->arrangeArray();
        if(empty($this->moneyId) || !is_numeric($this->moneyId) ){
            exception('未获到交款期数');
        }
        $data[$model->_status] = 1;
        $data[$model->_applyId] = 0;
        $data[$model->_applyTime] = time();
        $data[$model->_verifyId] = 0;
        $data[$model->_verifyTime] = time();
        $data[$model->_moneyId] = $this->moneyId;
        $data[$model->_type] = 'pay';
        return $this->save($data);
    }


    /**
     * 发起支付通知申请(需要审核的)
     * @return int|string
     * @throws \Exception
     */
    public function initiateOrderPayNotice(){
        $model = $this->getTable();
        $data = $this->arrangeArray();
        if(empty($this->moneyId) || !is_numeric($this->moneyId) ){
            exception('未获到交款期数');
        }
        $data[$model->_status] = 0;
        $data[$model->_applyId] = $this->opUid;
        $data[$model->_applyTime] = time();
        $data[$model->_type] = 'pay';
        $data[$model->_moneyId] = $this->moneyId;
        return $this->save($data);
    }

    /**
     * 发起签收通知
     * @return int|string
     * @throws \Exception
     */
    public function SignOrderNotice(){
        $model = $this->getTable();
        $data = $this->arrangeArray();
        $data[$model->_status] = 0;
        $data[$model->_applyId] = $this->opUid;
        $data[$model->_applyTime] = time();
        $data[$model->_type] = 'sign';
        return $this->save($data);
    }
    /**
     * 执行代码
     * @param $data
     * @return int|string
     * @throws \Exception
     */
    protected function save($data){
        $model = $this->getTable();
        $result =  $model->insert($data,false,true);
        if(!$result){
            exception('发起申请失败');
        }
        return $result;
    }
}